package com.yida.gwj.report.aspect;

import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.report.annotation.CalculateRateLog;
import com.yida.gwj.report.domain.GwjCalculateRateLog;
import com.yida.gwj.report.service.IGwjCalculateRateLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: PPV
 * @Date: 2023-03-17
 */
@Aspect
@Component
public class GwjCalculateRateLogAspect {

    @Resource
    private IGwjCalculateRateLogService logService;

    /**
     * 配置织入点
     */
    @Pointcut("@annotation(com.yida.gwj.report.annotation.CalculateRateLog)")
    public void pointCut() {

    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint
     * @param log
     */
    @AfterReturning(value = "pointCut()", returning = "log")
    public void doAfterReturning(JoinPoint joinPoint, GwjCalculateRateLog log) {
        //获取注解内容
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        CalculateRateLog annotation = signature.getMethod().getAnnotation(CalculateRateLog.class);
        //根据返回值，构建日志记录
        log.setCreateBy(SecurityUtils.getUsername());
        logService.saveLog(log, annotation.value());
    }

}
